<template>
	<div class="sys-pos-container">
		<el-card shadow="hover">
			<el-form :model="state.queryParams" ref="queryForm" :inline="true">
				<el-form-item :label-width='70' label="客户">
					<el-select :clearable='true' class='ml10' v-model="state.queryParams.customerId" placeholder="请选择"
						filterable>
						<el-option v-for='item in state.customerDict' :key='item.value' :label='item.label'
							:value='item.value' />
					</el-select>
				</el-form-item>
				<el-form-item :label-width='70' label="产品名称" prop="name">
					<el-input placeholder="产品名称" clearable class='ml10' @keyup.enter="handleQuery"
						v-model="state.queryParams.name" />
				</el-form-item>
				<el-form-item :label-width='70' label="运营商">
					<el-select class='ml10' v-model="state.queryParams.platformId" placeholder="请选择" filterable
						:clearable='true'>
						<el-option v-for='item in optionDict.operatorPlatformDict' :key='item.value' :label='item.label'
							:value='item.value' />
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-button-group>
						<el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'diCustomerProduct:query'">
							查询 </el-button>
						<el-button icon="ele-Refresh" @click="resetQuery" v-auth="'diCustomerProduct:query'"> 重置
						</el-button>
					</el-button-group>
				</el-form-item>
				<el-form-item>
					<el-button type="success" plain icon="ele-Plus" @click="openAdd" v-auth="'diCustomerProduct:add'"> 新增
					</el-button>
				</el-form-item>
			</el-form>
		</el-card>

		<el-card shadow="hover" style="margin-top: 8px">
			<el-table :data="state.data" style="width: 100%" v-loading="state.loading" border stripe highlight-current-row>
				<el-table-column header-align='center' type="index" label="序号" align="center" width="55"/>
				<el-table-column header-align='center' prop="customerName" label="客户名称" show-overflow-tooltip  width="240"/>
				<el-table-column header-align='center' prop="name" label="产品名称" show-overflow-tooltip width="300"/>
				<el-table-column header-align='center' prop="alias" label="产品简称" show-overflow-tooltip  width="240"/>
				<el-table-column header-align="center" prop="informationFee" label="信息费(元)" align="right"  width="90"
					show-overflow-tooltip>
					<template #default="scope">
						{{ (scope.row.informationFee / 100.00).toFixed(2) }}
					</template>
				</el-table-column>
				<el-table-column header-align="center" prop="informationFee" label="结算费(元)" align="right" width="90"
					show-overflow-tooltip>
					<template #default="scope">
						{{ (scope.row.settlementPrice / 100.00).toFixed(2) }}
					</template>
				</el-table-column>
				<el-table-column header-align='center' label="运营商" align="center" show-overflow-tooltip width="70">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.platformId === 1">移动</el-tag>
						<el-tag type="warning" v-else-if="scope.row.platformId === 2">联通</el-tag>
						<el-tag type="info" v-else-if="scope.row.platformId === 3">电信</el-tag>
						<el-tag type="error" v-else-if="scope.row.platformId === 4">广电</el-tag>
						<el-tag type="success" v-else-if="scope.row.platformId === 5">移动虚拟运营商</el-tag>
						<el-tag type="warning" v-else-if="scope.row.platformId === 6">联通虚拟运营商</el-tag>
						<el-tag type="info" v-else-if="scope.row.platformId === 7">电信虚拟运营商</el-tag>
						<el-tag type="error" v-else>未知</el-tag>
					</template>
				</el-table-column>
				<el-table-column header-align='center' label="状态" align="center" show-overflow-tooltip width="60">
					<template #default="scope">
						<div @click="changeStatus(scope.row)">
							<el-switch v-model="scope.row.status" :active-value="1" :inactive-value="2" size="small"
								style="--el-switch-on-color: #67C23A;" v-if="scope.row.status <= 2"
								:style="+scope.row.status > 2 ? '--el-switch-off-color: #E6A23C' : '--el-switch-off-color: red'"
								v-auth="'diCustomerProduct:update'" />
							<el-switch v-model="scope.row.status" :active-value="1" :inactive-value="4" size="small"
								style="--el-switch-on-color: #67C23A;--el-switch-off-color: #E6A23C"
								v-auth="'diCustomerProduct:update'" v-else />
						</div>
					</template>
				</el-table-column>
				<el-table-column header-align='center' label="计费结果" align="center" show-overflow-tooltip width="82">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.verifyPayResult">有</el-tag>
						<el-tag type="warning" v-else>无</el-tag>
					</template>
				</el-table-column>
				<el-table-column header-align='center' prop="requestLimit" label="请求上限" align='right' width="82"
					show-overflow-tooltip />
				<el-table-column header-align='center' prop="userLimit" label="用户上限" align='right' width="82" show-overflow-tooltip />
				<el-table-column header-align='center' prop="remark" label="备注" show-overflow-tooltip  min-width="300"/>
				<el-table-column header-align='center' label="操作" width="140" fixed="right" align="center"
					show-overflow-tooltip>
					<template #default="scope">
						<el-button size="small" text type="primary" @click="openEdit(scope.row, 'disabled')"
							v-auth="'diCustomerProduct:delete'"> 详情 </el-button>
						<el-button size="small" text type="primary" @click="openEdit(scope.row, 'edit')"
							v-auth="'diCustomerProduct:update'"> 编辑 </el-button>
						<el-button size="small" text type="danger" @click="del(scope.row)"
							v-auth="'diCustomerProduct:delete'"> 删除 </el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
				:page-sizes="[20, 50, 100]" v-model:current-page="state.queryParams.pageNum" background
				v-model:page-size="state.queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
				:total="state.total">
			</el-pagination>
		</el-card>
		<EditDiCustomerProduct ref="editDiCustomerProductRef" :title="state.editTitle" />
		<EditSynchronizationStatus ref="editSynchronizationStatusRef" :title="state.editTitle" @editStatus="handleQuery" />
	</div>
</template>

<script lang="ts" setup name="diCustomerProduct">
import { useI18n } from 'vue-i18n';
import { optionDict } from '/@/const/dict.ts';
import { onMounted, onUnmounted, reactive, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import EditDiCustomerProduct from '/@/views/delivery/operate/customerProduct/component/editDiCustomerProduct.vue';
import EditSynchronizationStatus from '/@/views/delivery/operate/customerProduct/component/editSynchronizationStatus.vue';
import { DiCustomerProduct } from '/@/api/models';
import SysDiCustomerProductApi from '/@/api/delivery/customerProduct';
const editSynchronizationStatusRef = ref<InstanceType<typeof EditSynchronizationStatus>>();
import SysDiCustomerApi from '/@/api/delivery/customer';

// 定义变量内容安全.
const { locale, t } = useI18n();
const editDiCustomerProductRef = ref<InstanceType<typeof EditDiCustomerProduct>>();
const state = reactive({
	loading: false,
	data: [] as Array<DiCustomerProduct>,
	total: 0 as any,
	queryParams: {
		name: undefined,
		pageNum: 1,
		pageSize: 20,
		customerId: undefined,
		platformId:undefined
	},
	editTitle: '',
	customerDict: [],
});

onMounted(async () => {
	mittBus.on('submitRefresh', () => {
		handleQuery();
	});
	await customerDictQuery();
	// console.log();

});



onUnmounted(() => {
	mittBus.off('submitRefresh');
});

// 查询操作
const handleQuery = async () => {
	state.loading = true;
	const res = await SysDiCustomerProductApi().queryRecord(state.queryParams);
	state.data = res.data.result.items ?? [];
	state.total = res.data.result?.total;
	state.loading = false;
};

// 重置操作
const resetQuery = () => {
	state.queryParams.name = undefined;
	state.queryParams.customerId = undefined;
	handleQuery();
};

// 业务列表
const customerDictQuery = async () => {
	await SysDiCustomerApi().queryAll({}).then((res) => {
		if (res.data.resCode === '0000')
			state.customerDict = res.data.result ?? [];
	});
};

// 打开新增页面
const openAdd = (type: string) => {
	state.editTitle = '添加';
	editDiCustomerProductRef.value?.openDialog({ port: "", status: 1, integral: 2000, chargingMode: 2, verifyPayResult: false, verifyCodeLength: "4,6", arrivalType: 2, requestLimit: 5000, userLimit: 500, priceExecutionTime: new Date(), httpMethod: 'GET', MimeType: '', syncPlanDaily: 1,blackPhone:-1 }, type);
};

// 打开编辑页面
const openEdit = (row: DiCustomerProduct, type: string) => {

	if (type === 'edit') {
		state.editTitle = '编辑';
		editDiCustomerProductRef.value?.openDialog(row, type);
	} else {
		state.editTitle = '详情';
		editDiCustomerProductRef.value?.openDialog(row, type);
	}
};

// 删除
const del = (row: any) => {
	ElMessageBox.confirm(`确定删除：【${row.name}】?`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res = await SysDiCustomerProductApi().deletePost({ id: row.id });
			if (res.data.resCode != '0000')
				ElMessage.error(res.data.resMessage);
			else
				ElMessage.success("删除成功");
			await handleQuery();
		})
		.catch(() => { });
};

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.queryParams.pageSize = val;
	handleQuery();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.queryParams.pageNum = val;
	handleQuery();
};

// 修改状态
const changeStatus = (row: any) => {
	state.editTitle = '提示';
	editSynchronizationStatusRef.value?.openDialog(row);

	// var onStatus: string = '';
	// if (row.status == 1) onStatus = '一键启动';
	// if (row.status == 2) onStatus = '一键关闭';

	// ElMessageBox.confirm(`是否${onStatus}（涉及到：产品计划->次日计划->当日计划）？`, '', {
	// 	title: t('message.user.logOutTitle'),
	// 	confirmButtonText: '确定',
	// 	cancelButtonText: '取消',
	// }
	// )
	// 	.then(() => {.
	// 		SysDiCustomerProductApi()
	// 			.setStatusPost({ id: row.id, status: row.status, syncPlanDaily: 1 })
	// 			.then(() => {
	// 				ElMessage.success(`${onStatus}状态设置成功`);
	// 			})
	// 			.catch(() => {
	// 				row.status = row.status == 1 ? 2 : 1;
	// 			});
	// 	})
	// 	.catch(() => {
	// 		row.status = row.status == 1 ? 2 : 1;
	// 	})
};

</script>
